<?php

namespace app\admin\controller\wms;

use app\admin\model\xmwms\Category;
use app\common\controller\Backend;

/**
 * 卡牌稀有度管理
 */
class Rating extends Backend
{
     /**
      * Rating模型对象
      * @var object
      * @phpstan-var \app\admin\model\wms\Rating
      */
     protected object $model;

     protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];

     protected string|array $quickSearchField = ['id'];

     public function initialize(): void
     {
          parent::initialize();
          $this->model = new \app\admin\model\wms\Rating;
     }


     /**
      * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
      */


     /**
      * 查看
      * @throws Throwable
      */
     public function index(): void
     {
          if ($this->request->param('select')) {
               $this->select();
          }

          list($where, $alias, $limit, $order) = $this->queryBuilder();
          $res = $this->model
               ->field($this->indexField)
               ->withJoin($this->withJoinTable, $this->withJoinType)
               ->alias($alias)
               ->where($where)
               ->order($order)
               ->paginate($limit);
          foreach ($res as &$value) {
               $value['category_name'] = Category::where(['id' => $value['category_id']])->value('name');
          }
          $this->success('', [
               'list' => $res->items(),
               'total' => $res->total(),
               'remark' => get_route_remark(),
          ]);
     }

     //获取一级分类下的稀有度
     public function getRatingByPid()
     {
          $data = $this->request->params([
               ["pid", ""],
          ]);
          $res = $this->model->where(['category_id' => $data['pid']])->select();
          $this->success('', [
               'list' => $res,
          ]);
     }
}
